package example.utils;

import okhttp3.*;

public class HttpClientUtil {

    private static final OkHttpClient client = new OkHttpClient();

    // 封装POST请求
    public static String post(String url, RequestBody formBody) throws Exception {
        Request request = new Request.Builder().url(url).post(formBody).build();
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            return response.body().string();
        }
    }

    // 封装GET请求
    public static String get(String url) throws Exception {
        Request request = new Request.Builder().url(url).build();
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            return response.body().string();
        }
    }
}
